754D - Fedor and coupons - CodeForces Solution


binary search data structures greedy sortings *2100

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

int n, k, l[300001], r[300001];

vector <pair <int, long long>> v;
bool possible (long long len) {
    v.clear();

    for (int i = 1; i <= n; ++ i) {
        if (r[i] - len + 1 < l[i]) {
            continue;
        }
        
        v.push_back ({l[i], 0});
        v.push_back ({r[i] - len + 2, 1});
    }

    sort (v.begin(), v.end());

    int sum = 0;
    for (int i = 0; i < (int)v.size() - 1; ++ i) {
        if (v[i].second) {
            -- sum;
        } else {
            ++ sum;
        }
        
        if (v[i].first != v[i + 1].first) {
            if (sum >= k) {
                return true;
            }
        }
    }

    return false;
}

void result (long long len) {
    cout << len << "\n";
    
    map <int, int> mp;

    for (int i = 1; i <= n; ++ i) {
        if (r[i] - len + 1 < l[i]) {
            continue;
        }

        mp[l[i]] ++;
        mp[r[i] - len + 2] --;
    }

    int sum = 0;
    int id = -1;

    for (auto it : mp) {
        sum += it.second;

        if (sum >= k) {
            id = it.first;
            break;
        }
    }

    if (id == -1) {
        for (int i = 1; i <= k; ++ i) {
            cout << i << " ";
        }
        cout << "\n";
        exit(0);
    }

    for (int i = 1; i <= n && k; ++ i) {
        if (l[i] <= id && id <= r[i] - len + 2) {
            cout << i << " ";
            -- k;
        }
    }

    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> k;
    for (int i = 1; i <= n; ++ i) {
        cin >> l[i] >> r[i];
    }

    long long l = 0, r = 2e9 + 1;
    while (l < r) {
        long long mid = (l + r + 1) >> 1;

        if (possible (mid)) {
            l = mid;
        } else {
            r = mid - 1;
        }
    }

    result (l);

    return 0;
}/*1690054665.6251605*/


Comments

Submit
0 Comments
More Questions

1529A - Eshag Loves Big Arrays
19. Remove Nth Node From End of List
925. Long Pressed Name
1051. Height Checker
695. Max Area of Island
402. Remove K Digits
97. Interleaving String
543. Diameter of Binary Tree
124. Binary Tree Maximum Path Sum
1465. Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts
501A - Contest
160A- Twins
752. Open the Lock
1535A - Fair Playoff
1538F - Interesting Function
1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String